package javamobile.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybeans.dao.DAOException;

import javamobile.model.*;

public class AcceptFriendAction extends Action{

	private DAOFactory factory;
	
	public AcceptFriendAction(Model model) {
		factory = model.getDAOFactory();
	}
	
	@Override
	public String getName() {return "accept.do";}

	@Override
	public Map<String, Object> perform(Map<String, Object> input) {
		System.out.println("Performing accept action!");
		Map<String, Object> output = new HashMap<String, Object>();
		List<String> errors = new ArrayList<String>();
		output.put("errors", errors);
		
		String username = (String) input.get("username");
		String email = (String) input.get("email");
		
		try {
			User user = factory.lookupUser(username);
			if (user == null) {
				errors.add("User "+username+" not found");
				return output;
			}
			factory.acceptRequest(user.getUserID(), email);
        }	catch (DAOException e) {
        	errors.add(e.getMessage());
        	return output;
	    }

		return output;
	}

}